package service

import (
	"grs-report-admin/model"
	"grs-report-admin/utils"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

func GetDataSetList(ctx *gin.Context) {
	dataSet := model.DataSetModel{}
	list, err := dataSet.List()
	if err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "查询失败",
			Result:  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, model.Result{
		Message: "查询成功",
		Result:  list,
	})
}
func AddDataSet(ctx *gin.Context) {
	dataSet := model.DataSetModel{}
	if err := ctx.ShouldBindJSON(&dataSet); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "添加失败",
			Result:  err.Error(),
		})
		return
	}
	dataSet.Id = utils.GetRowId()
	if err := dataSet.Insert(); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "添加失败",
			Result:  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, model.Result{
		Message: "添加成功",
		Result:  dataSet,
	})

}
func EditDataSet(ctx *gin.Context) {
	dataSet := model.DataSetModel{}
	if err := ctx.ShouldBindJSON(&dataSet); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "编辑失败",
			Result:  err.Error(),
		})
	}
	if err := dataSet.Update(); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "编辑失败",
			Result:  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, model.Result{
		Message: "编辑成功",
		Result:  dataSet,
	})
}
func DeleteDataSet(ctx *gin.Context) {
	dataSet := model.DataSetModel{}
	if err := ctx.ShouldBindJSON(&dataSet); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "删除失败",
			Result:  err.Error(),
		})
	}
	if err := dataSet.Delete(); err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "删除失败",
			Result:  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, model.Result{
		Message: "删除成功",
		Result:  dataSet,
	})
}

func DataSetExec(ctx *gin.Context) {
	dataSet := model.DataSetModel{}
	code := ctx.Query("code")
	log.Println(code)
	err := model.DB.Get(&dataSet, "SELECT * FROM data_set WHERE code='"+code+"'")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "查询数据集失败",
			Result:  err.Error(),
		})
		return
	}
	sql := dataSet.Exec
	if sql == "" {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "无效SQL",
			Result:  err.Error(),
		})
		return
	}
	rows, err := model.DB.Queryx(sql)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "查询失败",
			Result:  err.Error(),
		})
		return
	}
	sqlResult := []map[string]string{}
	columns, _ := rows.Columns()
	for rows.Next() {
		aReuslt := make(map[string]interface{})
		parseResult := make(map[string]string)
		rows.MapScan(aReuslt)
		for _, s := range columns {
			switch aReuslt[s].(type) {
			case []uint8:
				parseResult[s] = string(aReuslt[s].([]byte))
			}
		}
		sqlResult = append(sqlResult, parseResult)
	}
	if err != nil {
		ctx.JSON(http.StatusBadRequest, model.Result{
			Message: "查询失败",
			Result:  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, model.Result{
		Message: "查询成功",
		Result:  sqlResult,
	})
}
